package Day12.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class IsCopy {
    /**
     * 文件拷贝
     * @param source 文件源路径
     * @param dist  目标文件路径
     */
    public void copy(String source,String dist){
        FileInputStream fis = null;
        int n = 0;
        byte [] bt = new byte[2048];
        try {
            // 建立源对象
            fis = new FileInputStream(source);
            while (true){
                try {
                    // 读取数据
                    if (!((n = fis.read(bt)) != -1)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                FileOutputStream fos = null;
                // 建立目标对象
                fos = new FileOutputStream(dist);
                try {
                    fos.write(bt,0,n);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(fis != null){
                try {
                    // 关闭流
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
    IsCopy isCopy = new IsCopy();
    isCopy.copy("456.txt","4566.txt");
    }
}
